package Views;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import Controller.DBportal;
import Models.Ingredient;
import Models.Recipe;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int WINDOW_WIDTH = 650;
	public static final int WINDOW_HEIGHT = 550;

	private JPanel contentPane;
	private HomeView homeView;
	private RecipeView recipeView;
	
	private IngredientFrame maltFrame;
	private IngredientFrame hopsFrame;
	private IngredientFrame adjunctFrame;
	private IngredientFrame yeastFrame;
	
	private DBportal portal;
	private Recipe currentR;
	private Recipe parentR;


	/**
	 * Create the frame.
	 */
	public MainWindow(final DBportal portal) {
		this.portal = portal;
		currentR = new Recipe(); //create a blank recipe
		parentR = new Recipe();  //create a blank recipe
		
		setBackground(Color.WHITE);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); //let exitConfirmation() handle the closing
		//close database connections on exit
       addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
				exitConfirmation();
            }
        });
		
		setBounds(100, 100, WINDOW_WIDTH, WINDOW_HEIGHT);
		setTitle("Free Beer");
		
		createMenuBar();
		createTabs();
	}
	
	private void createTabs() {
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));

		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.setBackground(Color.WHITE);

		homeView = new HomeView(this,portal);
		homeView.setBackground(Color.WHITE);
		tabbedPane.insertTab("All", null, homeView, "All", 0);
		
		recipeView = new RecipeView(this);
		recipeView.setBackground(Color.WHITE);
		tabbedPane.insertTab("Recipe", null, recipeView, "Recipe", 1);
		
		NotesView nv = new NotesView();
		nv.setBackground(Color.WHITE);
		tabbedPane.insertTab("Notes", null, nv, "Notes", 2);
		
		FermentationView fv = new FermentationView();
		fv.setBackground(Color.WHITE);
		tabbedPane.insertTab("Fermentation", null, fv, "Fermentation", 3);
		
		contentPane.add(tabbedPane);
		setContentPane(contentPane);
	}
	
	/*
	 * Changes the recipes that are currently active.
	 */
	public void setRecipe(String idRecipe) {
		//sets the current to newCurrent
		//sets the parent to newCurrents parent
		this.currentR = portal.getRecipe(idRecipe);
		if(this.currentR.getIdParent() == null) {
			this.parentR = new Recipe();
		} else {
			this.parentR = portal.getRecipe(this.currentR.getIdParent());
		}
		recipeView.update(this.currentR, this.parentR);
		//TODO notifies all tabs to repaint
	}
	
	private void createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exitConfirmation();
			}
		});
		mnFile.add(mntmExit);
		
		JMenu mnCalculator = new JMenu("Calculator");
		menuBar.add(mnCalculator);
		
		JMenu Ingredients = new JMenu("Ingredients");
		menuBar.add(Ingredients);
		
		JMenuItem grainMnuItem = new JMenuItem(Ingredient.MALT);
		grainMnuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(maltFrame == null) {
					maltFrame = new IngredientFrame(portal.getIngredient(Ingredient.MALT));
					maltFrame.setTitle(Ingredient.MALT);
				}
				maltFrame.setVisible(true);
			}
			
		});
		Ingredients.add(grainMnuItem);
		
		JMenuItem hopsMnuItem = new JMenuItem(Ingredient.HOPS);
		Ingredients.add(hopsMnuItem);
		hopsMnuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(hopsFrame == null) {
					hopsFrame = new IngredientFrame(portal.getIngredient(Ingredient.HOPS));
					hopsFrame.setTitle(Ingredient.HOPS);
				}
				hopsFrame.setVisible(true);
			}
			
		});
		
		JMenuItem yeastMnuItem = new JMenuItem(Ingredient.YEAST);
		Ingredients.add(yeastMnuItem);
		yeastMnuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(yeastFrame == null) {
					yeastFrame = new IngredientFrame(portal.getIngredient(Ingredient.YEAST));
					yeastFrame.setTitle(Ingredient.YEAST);
				}
				yeastFrame.setVisible(true);
			}
		});
		
		JMenuItem adjunctMnuItem = new JMenuItem(Ingredient.ADJUNCT);
		Ingredients.add(adjunctMnuItem);
		adjunctMnuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(adjunctFrame == null) {
					adjunctFrame = new IngredientFrame(portal.getIngredient(Ingredient.ADJUNCT));
					adjunctFrame.setTitle(Ingredient.ADJUNCT);
				}
				adjunctFrame.setVisible(true);
			}
		});
		
		JMenuItem addMnuItem = new JMenuItem("Add Ingredient");
		Ingredients.add(addMnuItem);
		addMnuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddIngredientDialog dialog = new AddIngredientDialog();
				dialog.setVisible(true);
			}
		});
		
		JMenu menu = new JMenu("About");
		menuBar.add(menu);
	}
	
	private void exitConfirmation() {
		int option = JOptionPane.showConfirmDialog(null,"Continue?", "Are you sure?", JOptionPane.YES_NO_OPTION);
		if(option == JOptionPane.YES_OPTION) {
			portal.closeConnections();
			System.exit(0);
		}
	}
	

}
